package com.robotserver.orders.jobs;

import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.JdOrderSyncParam;
import com.robotserver.orders.param.VipOrderSyncParam;
import com.robotserver.orders.service.VipOrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: jianghan
 * @Date: 2020/12/2 11:28
 * @Description:
 */
@Component
public class VipOrderSyncJob {
    private static Logger logger = LoggerFactory.getLogger(VipOrderSyncJob.class);

    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private RedissonClient redissonClient;
    @Value("${cluster.group}")
    private String clusterGroup;

    /**
     * 每分钟执行一次拉取订单（时间范围是3分钟）
     */
//    @Scheduled(cron="0/30 * * * * ?")
    public void execute(){
        logger.debug("VipOrderSyncJob execute.");

        RLock lock = redissonClient.getLock(clusterGroup+"_service_vip_order_sync_lock");
        boolean getlock = false;
        try {
            if (getlock=lock.tryLock(0,1, TimeUnit.MINUTES)) {
                logger.debug("execute right.");
                Long tmptime = System.currentTimeMillis();
                Long end_time = tmptime ;                       //截至时间为当前时间
                Long begin_time = tmptime - 120000;             //起始时间为前2分钟
                VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
                vipOrderSyncParam.setBegin_time(begin_time);
                vipOrderSyncParam.setEnd_time(end_time);
                vipOrderService.syncOrder(vipOrderSyncParam);
            }else {
                logger.debug("not execute right.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (getlock){
                lock.unlock();
                logger.debug("release lock.");
            }
        }
    }


    /**
     * lucher
     * 生产者-往redis队列当中生产唯品会联盟
     * 每30秒执行一次查询唯品会联盟数据,然后保存到redis队列当中，充当生产者角色
     * @author lucher
     * @date 2021-03-13 10:06
     */
//    @Scheduled(cron="0/30 * * * * ?")
    public void scheduled_vipTokens_producer(){
        logger.info("-----VipOrderSyncJob scheduled_vipTokens_producer  start----");

        RLock lock = redissonClient.getLock("VipOrderSyncJob_scheduled_vipTokens_producer");
        boolean getlock = false;
        try {
            if (getlock=lock.tryLock()) {
                logger.info("---- execute right----");
                Long tmptime = System.currentTimeMillis();
                Long end_time = tmptime ;                       //截至时间为当前时间
                Long begin_time = tmptime - 120000;             //起始时间为前2分钟
                VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
                vipOrderSyncParam.setBegin_time(begin_time);
                vipOrderSyncParam.setEnd_time(end_time);
                vipOrderSyncParam.setRedisQueueKey(CacheKey.vipPayOrderQueue);
                //调用生产者线程，遍历唯品会联盟，往redis队列中添加vipTokens
                vipOrderService.syncOrder_producer(vipOrderSyncParam);
                logger.info("-------------往redis中推入唯品会联盟------vipOrderService=" + vipOrderService.toString());
            }else {
                logger.debug("----not execute right----");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (getlock){
                lock.unlock();
                logger.info("----release lock----");
            }
        }
    }

    /**
     * lucher
     * 消费者-往redis队列当中消费vipTokens
     * 每10秒执行一次定时任务，启动线程从redis队列中获取vipTokens
     * @author lucher
     * @date 2021-03-13 10:06
     */
//    @Scheduled(cron="0/30 * * * * ?")
    public void scheduled_vipTokens_consumer(){
        logger.info("-----VipOrderSyncJob scheduled_vipTokens_consumer  start----");
        //调用消费者线程，遍历redis队列，消费vipTokens
        VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
        vipOrderSyncParam.setRedisQueueKey(CacheKey.vipPayOrderQueue);
        vipOrderService.syncOrder_consumer(vipOrderSyncParam);
        logger.info("---------------------------消费唯品会联盟-----------------vipOrderService=" + vipOrderService.toString());
    }

}
